package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.H5User;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.UserSettlementMapper;
import com.ruoyi.web.domain.UserSettlement;
import com.ruoyi.web.service.IUserSettlementService;

/**
 * 结算单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-11
 */
@Service
public class UserSettlementServiceImpl implements IUserSettlementService 
{
    @Autowired
    private UserSettlementMapper userSettlementMapper;

    /**
     * 查询结算单
     * 
     * @param id 结算单主键
     * @return 结算单
     */
    @Override
    public UserSettlement selectUserSettlementById(Long id)
    {
        return userSettlementMapper.selectUserSettlementById(id);
    }

    /**
     * 查询结算单列表
     * 
     * @param userSettlement 结算单
     * @return 结算单
     */
    @Override
    public List<UserSettlement> selectUserSettlementList(UserSettlement userSettlement)
    {
        if(StringUtils.equals("h5", SecurityUtils.getClientType())){
           H5User h5User= SecurityUtils.getLoginUser().getH5User();
           if(h5User.getTop().equals(0L)){
            userSettlement.setCompanyId(h5User.getId());
           }else{
            userSettlement.setCompanyId(h5User.getTop());
           }
        }
        return userSettlementMapper.selectUserSettlementList(userSettlement);
    }

    /**
     * 新增结算单
     * 
     * @param userSettlement 结算单
     * @return 结果
     */
    @Override
    public int insertUserSettlement(UserSettlement userSettlement)
    {
        userSettlement.setCreateTime(DateUtils.getNowDate());
        if(StringUtils.equals("h5", SecurityUtils.getClientType())){
            userSettlement.setCreator(SecurityUtils.getLoginUser().getUserId());

            H5User h5User = SecurityUtils.getLoginUser().getH5User();
            if(h5User.getTop().equals(0L)){
                userSettlement.setCompanyId(h5User.getId());
            }else{
            userSettlement.setCompanyId(h5User.getTop());

            }
        }else{
            userSettlement.setCreator(Long.parseLong(SecurityUtils.getLoginUser().getUser().getBindUserId()));
        }
        return userSettlementMapper.insertUserSettlement(userSettlement);
    }

    /**
     * 修改结算单
     * 
     * @param userSettlement 结算单
     * @return 结果
     */
    @Override
    public int updateUserSettlement(UserSettlement userSettlement)
    {
        userSettlement.setUpdateTime(DateUtils.getNowDate());
        return userSettlementMapper.updateUserSettlement(userSettlement);
    }

    /**
     * 批量删除结算单
     * 
     * @param ids 需要删除的结算单主键
     * @return 结果
     */
    @Override
    public int deleteUserSettlementByIds(Long[] ids)
    {
        return userSettlementMapper.deleteUserSettlementByIds(ids);
    }

    /**
     * 删除结算单信息
     * 
     * @param id 结算单主键
     * @return 结果
     */
    @Override
    public int deleteUserSettlementById(Long id)
    {
        return userSettlementMapper.deleteUserSettlementById(id);
    }

    /**
     * 根据订单id删除结算单
     * 
     * @param orderId 订单id
     * @return 结果
     */
    @Override
    public int deleteUserSettlementByOrderId(Long orderId)
    {
        return userSettlementMapper.deleteUserSettlementByOrderId(orderId);
    }
}
